package com.ansion.utils;

import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;

import com.ansion.basetool.R;


/**
 * created by zhaoyuntao
 * on 06/12/2020
 * description:
 */
public class ColorStringUtils {
    public static SpannableString get(String content, int color, int textSize) {
        if (TextUtils.isEmpty(content)) {
            content = "null";
        }
        SpannableString spannableString = new SpannableString(content);
        if (textSize > 0) {
            spannableString.setSpan(new AbsoluteSizeSpan(textSize, true), 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        spannableString.setSpan(getColorSpan(color), 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static ForegroundColorSpan getColorSpan(int color) {
        return new ForegroundColorSpan(color);
    }

    public static SpannableStringBuilder setKeyWordHighlight(CharSequence content, String keywords, Integer color) {
        if (TextUtils.isEmpty(content)) {
            content = "";
        }
        SpannableStringBuilder s = new SpannableStringBuilder(content);

        if (TextUtils.isEmpty(keywords)) {
            return s;
        }
        if (color == null) {
            color = ResourceUtils.getColor(R.color.sky_blue_3);
        }

        try {
            int keyLen = keywords.length();
            int fromIndex = 0;
            int start = -1;
            int end = -1;

            String keyCheck = keywords.toLowerCase();
            String stCheck = content.toString().toLowerCase();
            while (true) {
                start = stCheck.indexOf(keyCheck, fromIndex);
                if (start < 0) {
                    break;
                }

                end = start + keyLen;
                s.setSpan(
                        getColorSpan(color),
                        start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                fromIndex = end;
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return s;
    }

    public static SpannableStringBuilder setKeyWordHighlightBold(CharSequence content, String keywords, Integer color, boolean isBold) {
        if (TextUtils.isEmpty(content)) {
            content = "";
        }
        SpannableStringBuilder s = new SpannableStringBuilder(content);

        if (TextUtils.isEmpty(keywords)) {
            return s;
        }
        if (color == null) {
            color = ResourceUtils.getColor(R.color.sky_blue_3);
        }

        try {
            int keyLen = keywords.length();
            int fromIndex = 0;
            int start = -1;
            int end = -1;

            String keyCheck = keywords.toLowerCase();
            String stCheck = content.toString().toLowerCase();
            while (true) {
                start = stCheck.indexOf(keyCheck, fromIndex);
                if (start < 0) {
                    break;
                }

                end = start + keyLen;
                if (isBold) {
                    //加粗
                    s.setSpan(
                            new FakeBoldSpan(),
                            start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
                s.setSpan(
                        getColorSpan(color),
                        start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                fromIndex = end;
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return s;
    }
}
